package edu.seu.mart.controller;

import edu.seu.mart.model.Admin;
import edu.seu.mart.model.Goods;
import edu.seu.mart.model.Knowledge;
import edu.seu.mart.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by asus on 2015/3/6.
 */

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private IAdminService adminService;
    @Resource
    private IUserService userService;
    @Resource
    private ISellerService sellerService;
    @Resource
    private IGoodsService goodsService;
    @Resource
    private IChatService chatService;

    private String order="desc";

    @RequestMapping("/index")
    public String index() {
        return "admin/index";
    }

    @RequestMapping(value="/login",method= RequestMethod.GET)
    public String login() {
        return "admin/login";
    }

    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(String username,String password,Model model,HttpSession session) throws NoSuchAlgorithmException {



        Admin admin =adminService.login(username, password);
        if(admin!=null){
            session.setAttribute("admin", admin);
            return "redirect:/admin/index";
        }else{
            return "redirect:admin/login";
        }

    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("admin");
        session.invalidate();
        return "admin/login";
    }

    /**
     * 用户管理模块
     */
    @RequestMapping("/userList")
    public String userList(Model model) {
        model.addAttribute("userList",userService.findUser());
        return "admin/userList";
    }

    @RequestMapping(value="/user/delete/{id}",method=RequestMethod.GET)
    public String userDelete(@PathVariable int id) {
        System.out.println(id );
        userService.delete(id);
        return "redirect:/admin/userList";
    }

    @RequestMapping(value="/user/updateStatus/{id}",method=RequestMethod.GET)
    public String userUpdateStatus(@PathVariable int id) {
        userService.updateStatus(id);
        return "redirect:/admin/userList";
    }

    @RequestMapping(value="/userList",method=RequestMethod.POST)
    public String userDeleteChecked(@RequestParam("ids") String[] ids ) {
        userService.deleteChecked(ids);
        return "redirect:/admin/userList";
    }
    @RequestMapping(value="/test",method=RequestMethod.POST)
    public String test(@RequestParam("ids") String[] ids ) {
        for(String str:ids){
            System.out.println(str);
        }
        return "redirect:/admin/userList";
    }

    @RequestMapping(value="/user/search",method=RequestMethod.POST)
    public String userSearch(@RequestParam("name") String name ,Model model) {
        model.addAttribute("userList", userService.findUser(name));
        return "admin/userList";
    }

    /**
     * 商家管理模块
     */
    @RequestMapping("/sellerList")
    public String sellerList(Model model) {
        model.addAttribute("sellList",sellerService.findUser(order));

        return "admin/sellerList";
    }

    @RequestMapping(value="/seller/delete/{id}",method=RequestMethod.GET)
    public String sellerDelete(@PathVariable int id) {
        sellerService.delete(id);
        return "redirect:/admin/sellerList";
    }
    @RequestMapping(value="/seller/updateStatus/{id}",method=RequestMethod.GET)
    public String sellerUpdateStatus(@PathVariable int id) {
        sellerService.updateStatus(id);
        return "redirect:/admin/sellerList";
    }

    @RequestMapping(value="/sellerList",method=RequestMethod.POST)
    public String sellerDeleteChecked(@RequestParam("ids") String[] ids ) {
        sellerService.deleteChecked(ids);
        return "redirect:/admin/sellerList";
    }
    @RequestMapping(value="seller/changeOrder")
    public String changeOrder() {
        order=order.equals("desc") ? "asc":"desc";
        return "redirect:/admin/sellerList";
    }
    @RequestMapping(value="/seller/search",method=RequestMethod.POST)
    public String sellerSearch(@RequestParam("name") String name ,Model model) {
        model.addAttribute("sellList", sellerService.findSeller(name));
        return "admin/sellerList";
    }
    /**
     * 商品管理模块
     */
    @RequestMapping("/goodsList")
    public String goodsList(Model model) {
        model.addAttribute("goodsList",goodsService.findGoods());

        return "admin/goodsList";
    }
    @RequestMapping("/sellGoodList")
    public String sellGoodList(Model model) {
        model.addAttribute("goodsList",goodsService.findGoodsList(1));

        return "admin/goodsList";
    }
    @RequestMapping("/unSellGoodList")
    public String unSellGoodList(Model model) {
        model.addAttribute("goodsList",goodsService.findGoodsList(0));

        return "admin/goodsList";
    }
    @RequestMapping(value="/goods/search",method=RequestMethod.POST)
    public String goodsSearch(@RequestParam("name") String name ,Model model) {
        model.addAttribute("goodsList", goodsService.findGoodsByNameOrBrand(name));
        return "admin/goodsList";
    }
    @RequestMapping(value="/goods/updateStatus/{id}",method=RequestMethod.GET)
    public String goodsUpdateStatus(@PathVariable int id) {
        goodsService.updateStatus(id);
        return "redirect:/admin/goodsList";
    }
    @RequestMapping(value="/goods/updateUnPassStatus/{id}",method=RequestMethod.GET)
    public String updateUnPassStatus(@PathVariable int id) {
        goodsService.updateUnPassStatus(id);
        return "redirect:/admin/goodsList";
    }
    @RequestMapping(value="/goods/delete/{id}",method=RequestMethod.GET)
    public String goodsDelete(@PathVariable int id) {
        goodsService.delete(id);
        return "redirect:/admin/goodsList";
    }
    @RequestMapping(value="/goodsList",method=RequestMethod.POST)
    public String goodsDeleteChecked(@RequestParam("ids") String[] ids ) {
        goodsService.deleteChecked(ids);
        return "redirect:/admin/goodsList";
    }
    @RequestMapping(value="/goodsDetail/{id}",method=RequestMethod.GET)
    public String goodsDetail(@PathVariable int id ,Model model)
    {
         Goods goods=goodsService.load(id);
         model.addAttribute("goods", goods);
         List<String> fourImageList=new ArrayList<String>();
         String fourImageStr=goods.getFourImage();
         Collections.addAll(fourImageList, fourImageStr.split(";"));
        for(String str:fourImageList){
            System.out.println(str);
        }
         List<String> detailImageList=new ArrayList<String>();
         String detailImageStr=goods.getDetailImage();
         Collections.addAll(detailImageList, detailImageStr.split(";"));
        for(String str:detailImageList){
            System.out.println(str);
        }
         model.addAttribute("fourImageList", fourImageList);
         model.addAttribute("detailImageList", detailImageList);
         return "/admin/goodsDetail";
    }
    /**
     * 知识库管理
     */
    @RequestMapping(value="/knowledgeList",method=RequestMethod.GET)
    public String knowledgeList(Model model) {
        model.addAttribute("knowledgeList",chatService.findAllPagerKnowledge());
        System.out.println();
        return "/admin/knowledgeList";
    }
    @RequestMapping(value="/knowledgeUpdate",method=RequestMethod.POST)
      public String knowledgeUpdate(@RequestParam("knowledgeId") int knowledgeId ,@RequestParam("question") String question ,@RequestParam("answer") String answer ,Model model) {
//        model.addAttribute("knowledgeList",chatService.findAllKnowledge());
        Knowledge knowledge=chatService.load(knowledgeId);
        if(knowledge!=null){
            knowledge.setQuestion(question);
            knowledge.setAnswer(answer);
            chatService.update(knowledge);
        }
        return "redirect:/admin/knowledgeList";
    }
    @RequestMapping(value="/knowledgeAdd",method=RequestMethod.POST)
     public String knowledgeAdd( @RequestParam("question") String question ,@RequestParam("answer") String answer ,Model model) {
//        model.addAttribute("knowledgeList",chatService.findAllKnowledge());
        Knowledge knowledge=new Knowledge();

        knowledge.setQuestion(question);
        knowledge.setAnswer(answer);
        chatService.save(knowledge);

        return "redirect:/admin/knowledgeList";
    }
    @RequestMapping(value="/knowledgeDelete/{id}",method=RequestMethod.GET)
    public String knowledgeDelete(@PathVariable int id ) {
//        model.addAttribute("knowledgeList",chatService.findAllKnowledge());

            chatService.delete(id);
        return "redirect:/admin/knowledgeList";
    }
}
